using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Timers;

namespace ICP_MS;

public class ExperimentService
{
	public Method method = new Method();

	public double[] PeakMass = null;

	public int[] ResolutionType = null;

	public double[] Dwelltimes = null;

	private List<double> OutPeakMass = new List<double>();

	private List<double> OutPeakMassBuffer = new List<double>();

	private SpectrumInfos CurrentSpectrums = new SpectrumInfos();

	public bool IsRun = false;

	public bool IsStable = false;

	public System.Timers.Timer timer = new System.Timers.Timer();

	private AutoResetEvent scanEvent = new AutoResetEvent(initialState: false);

	public bool bStableFunEnd = false;

	public Platform CurrentPlat { get; set; }

	public X2Lens CurrentLens { get; set; }

	public PlasmaPara CurrentPlasma { get; set; }

	public RFS CurrentRFS { get; set; }

	public Injector CurrentInjector { get; set; }

	public static event EventHandler<iMassDataEventArgs> StatusEvent;

	private void LoadConfigration(string name)
	{
		CurrentPlat = PlatManager.plat.Clone();
		CurrentLens = LensManager.x2lens.Clone();
		CurrentPlasma = PlasmaManager.plasma.Clone();
		CurrentRFS = RFManager.rfs.Clone();
		CurrentInjector = PlatManager.injector.Clone();
		int platId = CurrentPlat.id;
		int lensId = CurrentLens.id;
		int plasmaId = CurrentPlasma.Id;
		int rfsId = CurrentRFS.id;
		InstrumentConfig config = DataBase.Instance.GetInstrumentConfig(name);
		if (config != null)
		{
			name = config.Time;
		}
		X2Lens lens = DataBase.Instance.GetX2Lens(name);
		if (lens != null)
		{
			CurrentLens = lens;
			CurrentLens.id = lensId;
		}
		else
		{
			Log.InsertLog("Configration", "Load lens params error.", "", Severity.Error);
		}
		PlasmaPara plasma = DataBase.Instance.GetPlasma(name);
		if (plasma != null)
		{
			CurrentPlasma = plasma;
			CurrentPlasma.Id = plasmaId;
		}
		else
		{
			Log.InsertLog("Configration", "Load plasma params error.", "", Severity.Error);
		}
		RFS rfs = DataBase.Instance.GetRFS(name);
		if (rfs != null)
		{
			CurrentRFS = rfs;
			CurrentRFS.id = rfsId;
		}
		else
		{
			Log.InsertLog("Configration", "Load rf params error.", "", Severity.Error);
		}
		InstrumentInjector injector = DataBase.Instance.GetInjector(name);
		if (injector != null)
		{
			CurrentInjector.Perist = injector.Perist;
		}
		else
		{
			PlatManager.Instrumentinjector.Perist = 350;
		}
		Platform plat = DataBase.Instance.GetPlatform(name);
		if (plat != null)
		{
			CurrentPlat = plat.Clone();
		}
		else
		{
			Log.InsertLog("Configration", "Load platform params error.", "", Severity.Error);
		}
	}

	public void OnStatus(string msg)
	{
		if (ExperimentService.StatusEvent != null)
		{
			ExperimentService.StatusEvent(null, new iMassDataEventArgs(msg));
		}
	}

	public void StartAnalysis()
	{
		try
		{
			IsRun = true;
			Detector.errorevent += Detector_errorevent;
			Detector.finishReceive += Detector_finishReceive;
			Detector.errorReceive += Detector_errorReceive;
			PeakMass = method.Masses.ToArray();
			Dwelltimes = method.Dwells.ToArray();
			ResolutionType = new int[PeakMass.Length];
			for (int i = 0; i < PeakMass.Length; i++)
			{
				ResolutionType[i] = 1;
			}
			if (!Detector.detectorConfig.IsNewBoard)
			{
				scanEvent.Reset();
				if (ExperimentForStable())
				{
					scanEvent.Reset();
					ExperimentMainRun();
				}
				return;
			}
			scanEvent.Reset();
			if (ExperimentForStableForNew())
			{
				scanEvent.Reset();
				ExperimentMainRunForNew();
				iMassProcess.Instance.SetClientEvent();
			}
		}
		catch (Exception)
		{
		}
		finally
		{
			Detector.errorevent -= Detector_errorevent;
			Detector.finishReceive -= Detector_finishReceive;
			Detector.errorReceive -= Detector_errorReceive;
			StopRun();
		}
	}

	private void Detector_errorevent(object sender, EventArgs e)
	{
		StopRun();
	}

	public void ShowMsg(string msg)
	{
		iMassProcess.ShowMsg(msg);
	}

	public void StopRun()
	{
		timer.Stop();
		IsRun = false;
		IsStable = false;
		Detector.Instance.Stop();
		scanEvent.Set();
	}

	private void Detector_finishReceive(object sender, EventArgs e)
	{
		scanEvent.Set();
	}

	private void Detector_errorReceive(object sender, ErrorMsgEventArgs e)
	{
		if (!OutPeakMass.Contains(e.X) && !OutPeakMassBuffer.Contains(e.X))
		{
			OutPeakMassBuffer.Add(e.X);
		}
	}

	public bool SetConfig()
	{
		try
		{
			string name = ((method.TuneMode == 0) ? "标准模式" : "KED模式");
			ShowMsg("加载调谐模式：" + name);
			LoadConfigration(name);
			LensManager.x2lens = CurrentLens.Clone();
			LensManager.Instance.X2_SetAll();
			int statusPlasma = PlasmaManager.plasma.StatusPlasma;
			PlasmaManager.plasma = CurrentPlasma.Clone();
			PlasmaManager.plasma.StatusPlasma = statusPlasma;
			Plasma.Instance.OpenCoolGas();
			Plasma.Instance.OpenNebulizerGas();
			Plasma.Instance.SetPower();
			if (Collision.Instance.CRCType == 1)
			{
				Plasma.Instance.OpenCollision();
			}
			else
			{
				Plasma.Instance.OpenCollision();
				Plasma.Instance.OpenCollision2();
			}
			Plasma.Instance.OpenAppendixGas();
			double dwell = RFManager.rfs.dwell;
			RFManager.rfs = CurrentRFS.CloneWithoutDwell();
			RFManager.rfs.dwell = dwell;
			Detector.Instance.msDetectorRF.SetSuspend(useFactor: false);
			Detector.Instance.msDetectorRF.SetStable();
			if (Detector.detectorConfig.IsNewDetector)
			{
				Detector.Instance.msDetectorRF.SetQuadPoleNew();
				Detector.Instance.msDetectorRF.SetPrePoleNew();
				Detector.Instance.msDetectorRF.SetHVNNew();
				Detector.Instance.msDetectorRF.SetHVPNew();
				Detector.Instance.msDetectorRF.SetGateNew();
			}
			else
			{
				Detector.Instance.msDetectorRF.SetQuadPole();
				Detector.Instance.msDetectorRF.SetPrePole();
				Detector.Instance.msDetectorRF.SetHVN();
				Detector.Instance.msDetectorRF.SetHVP();
				Detector.Instance.msDetectorRF.SetGate();
			}
			PlatManager.plat = CurrentPlat.Clone();
			Plasma.Instance.SetPlatform();
			PlatManager.Instrumentinjector.Perist = CurrentInjector.Clone().Perist;
			Plasma.Instance.SetPeristaltic(PlatManager.Instrumentinjector.Perist);
			ShowMsg("设置调谐模式成功");
			return true;
		}
		catch (Exception e)
		{
			ShowMsg($"设置调谐模式失败：{e.Message}");
			return false;
		}
	}

	public bool ExperimentForStable()
	{
		bStableFunEnd = false;
		OutPeakMass.Clear();
		OutPeakMassBuffer.Clear();
		timer.Interval = 30000.0;
		timer.Elapsed += Timer_Elapsed;
		timer.Start();
		IsStable = true;
		while (IsRun && IsStable)
		{
			OnStatus("正在运行稳定程序");
			List<double> mass2 = new List<double>();
			List<int> resolution1 = new List<int>();
			double[] mass = PeakMass.ToArray();
			if (OutPeakMass != null || OutPeakMass.Count() > 0)
			{
				for (int i = 0; i < mass.Count(); i++)
				{
					if (!OutPeakMass.Contains(mass[i]))
					{
						mass2.Add(mass[i]);
						resolution1.Add(ResolutionType[i]);
					}
				}
			}
			mass = mass2.ToArray();
			Detector.Instance.SetMassAxis(mass);
			Detector.Instance.SendHopDatas(mass, resolution1.ToArray(), start: true);
			if (!scanEvent.WaitOne(60000))
			{
				GenerateJson.ReturnData(1, 1, 0, 0, 0, null);
				Log.InsertLog(Lang.Txt("FormExperLog.Text1"), Lang.Txt("FormExperLog.Text2"), Lang.Txt("FormExperLog.Text3"), Severity.Error);
				return false;
			}
			OnStatus(GenerateJson.ReturnData(0, 0, 0, 0, 0, Detector.msDetectorPara.IntesnsityPulse.ToList()));
			OutPeakMass.AddRange(OutPeakMassBuffer);
			OutPeakMassBuffer.Clear();
		}
		bStableFunEnd = true;
		return true;
	}

	private void Timer_Elapsed(object sender, ElapsedEventArgs e)
	{
		timer.Stop();
		IsStable = false;
	}

	public bool ExperimentForStableForNew()
	{
		ShowMsg("四极杆正在稳定30s...");
		bStableFunEnd = false;
		RFManager.rfs.Gate = 0f;
		if (Detector.detectorConfig.IsNewDetector)
		{
			Detector.Instance.msDetectorRF.SetGateNew();
		}
		else
		{
			Detector.Instance.msDetectorRF.SetGate();
		}
		Thread.Sleep(1000);
		List<double> mass2 = new List<double>();
		List<int> resolution1 = new List<int>();
		double[] mass = PeakMass.ToArray();
		for (int i = 0; i < mass.Count(); i++)
		{
			mass2.Add(mass[i]);
			resolution1.Add(ResolutionType[i]);
		}
		double[] intensityANA = new double[mass.Length];
		int count = 1;
		OutPeakMass.Clear();
		OutPeakMassBuffer.Clear();
		timer.Interval = 30000.0;
		timer.Elapsed += Timer_Elapsed;
		timer.Start();
		IsStable = true;
		while (IsRun && IsStable)
		{
			Detector.Instance.SetMassAxis(mass2.ToArray());
			Detector.Instance.SendHopDatas(mass2.ToArray(), resolution1.ToArray(), start: true);
			if (!scanEvent.WaitOne(60000))
			{
				string jsontext2 = GenerateJson.ReturnData(1, 0, 0, 0, 0, null);
				OnStatus(jsontext2);
				iMassProcess.Instance.SetClientEvent();
				ShowMsg(Lang.Txt("FormExperLog.Text2") + "...");
				Log.InsertLog("客户端系统", "稳定采集出错", Lang.Txt("FormExperLog.Text3"), Severity.Error);
				return false;
			}
			for (int k = 0; k < mass.Length; k++)
			{
				intensityANA[k] += Detector.msDetectorPara.IntesnsityAnalog[k];
			}
			if (count >= 10)
			{
				break;
			}
			count++;
			Thread.Sleep(10);
		}
		for (int j = 0; j < mass.Length; j++)
		{
			if (intensityANA[j] / (double)count * Detector.Instance.dacPAFactors[(int)(mass[j] * 100.0)] < Detector.detectorConfig.PaThreshold)
			{
				OutPeakMass.Add(mass[j]);
			}
		}
		if (!IsRun || !IsStable)
		{
			return true;
		}
		while (IsRun && IsStable)
		{
			ShowMsg("四极杆正在稳定...");
			Detector.Instance.SetMassAxis(mass2.ToArray());
			Detector.Instance.SendHopDatas(mass2.ToArray(), resolution1.ToArray(), start: true);
			if (!scanEvent.WaitOne(60000))
			{
				string jsontext = GenerateJson.ReturnData(1, 0, 0, 0, 0, null);
				OnStatus(jsontext);
				iMassProcess.Instance.SetClientEvent();
				ShowMsg(Lang.Txt("FormExperLog.Text2") + "...");
				Log.InsertLog(Lang.Txt("FormExperLog.Text1"), Lang.Txt("FormExperLog.Text2"), Lang.Txt("FormExperLog.Text3"), Severity.Error);
				return false;
			}
			Thread.Sleep(10);
		}
		bStableFunEnd = true;
		return true;
	}

	public bool ExperimentMainRun()
	{
		for (int runIndex = 0; runIndex < method.ScanTimes; runIndex++)
		{
			if (!IsRun)
			{
				return true;
			}
			double[] mass = PeakMass.ToArray();
			List<double> mass2 = new List<double>();
			List<int> resolution1 = new List<int>();
			if (OutPeakMass != null || OutPeakMass.Count() > 0)
			{
				for (int i = 0; i < mass.Count(); i++)
				{
					if (!OutPeakMass.Contains(mass[i]))
					{
						mass2.Add(mass[i]);
						resolution1.Add(ResolutionType[i]);
					}
				}
			}
			mass = mass2.ToArray();
			for (int sweepIndex = 0; sweepIndex < method.Sweeps; sweepIndex++)
			{
				if (!IsRun)
				{
					return true;
				}
				if (sweepIndex == 0)
				{
					Detector.Instance.SetMassAxis(mass);
				}
				Detector.Instance.SendHopDatas(mass, Dwelltimes, resolution1.ToArray(), start: true);
				if (!scanEvent.WaitOne(60000))
				{
					string jsontext1 = GenerateJson.ReturnData(1, 0, 0, 0, 0, null);
					OnStatus(jsontext1);
					Log.InsertLog(Lang.Txt("FormExperLog.Text4"), Lang.Txt("FormExperLog.Text2"), Lang.Txt("FormExperLog.Text3"), Severity.Error);
					return false;
				}
			}
			ProcessSpectums(CurrentSpectrums, runIndex, method.Sweeps);
			ProcessMainRunDatas(CurrentSpectrums, runIndex);
		}
		return true;
	}

	private bool ExperimentMainRunForNew()
	{
		for (int runIndex = 0; runIndex < method.ScanTimes; runIndex++)
		{
			if (!IsRun)
			{
				return true;
			}
			double[] mass = PeakMass.ToArray();
			List<double> mass2 = new List<double>();
			List<int> resolution1 = new List<int>();
			List<double> intensityPC = new List<double>();
			List<double> intensityANAforPC = new List<double>();
			if (OutPeakMass != null || OutPeakMass.Count() > 0)
			{
				for (int j = 0; j < mass.Count(); j++)
				{
					if (OutPeakMass.Contains(mass[j]))
					{
						mass2.Add(mass[j]);
						resolution1.Add(ResolutionType[j]);
					}
				}
				mass = mass2.ToArray();
				if (mass.Count() > 0)
				{
					RFManager.rfs.Gate = (float)((double)RFManager.rfs.HVP * 0.23);
					if (Detector.detectorConfig.IsNewDetector)
					{
						Detector.Instance.msDetectorRF.SetGateNew();
					}
					else
					{
						Detector.Instance.msDetectorRF.SetGate();
					}
					int count2 = 0;
					while (IsRun && !(Math.Abs(RFManager.rfs.ReadGate - RFManager.rfs.Gate) < 20f) && count2 != 30)
					{
						Thread.Sleep(100);
						count2++;
					}
					ShowMsg("PC模式 正在采集数据...");
					for (int sweepIndex2 = 0; sweepIndex2 < method.Sweeps; sweepIndex2++)
					{
						ShowMsg($"扫描第{runIndex + 1}次，重复次数{sweepIndex2 + 1}");
						if (!IsRun)
						{
							return true;
						}
						if (sweepIndex2 == 0)
						{
							Detector.Instance.SetMassAxis(mass);
						}
						Detector.Instance.SendHopDatas(mass, Dwelltimes, resolution1.ToArray(), start: true);
						if (!scanEvent.WaitOne(60000))
						{
							string jsontext2 = GenerateJson.ReturnData(1, 0, 0, 0, 0, null);
							OnStatus(jsontext2);
							ShowMsg("PC模式，采集出错");
							Log.InsertLog(Lang.Txt("FormExperLog.Text4"), Lang.Txt("FormExperLog.Text2"), Lang.Txt("FormExperLog.Text3"), Severity.Error);
							return false;
						}
					}
					intensityPC = Detector.msDetectorPara.IntesnsityPulse.ToList();
					intensityANAforPC = Detector.msDetectorPara.IntesnsityAnalog.ToList();
				}
			}
			if (!IsRun)
			{
				return true;
			}
			List<double> intensityANA = new List<double>();
			mass2.Clear();
			resolution1.Clear();
			mass = PeakMass.ToArray();
			for (int i = 0; i < mass.Count(); i++)
			{
				if (!OutPeakMass.Contains(mass[i]))
				{
					mass2.Add(mass[i]);
					resolution1.Add(ResolutionType[i]);
				}
			}
			mass = mass2.ToArray();
			if (mass.Count() > 0)
			{
				RFManager.rfs.Gate = 0f;
				if (Detector.detectorConfig.IsNewDetector)
				{
					Detector.Instance.msDetectorRF.SetGateNew();
				}
				else
				{
					Detector.Instance.msDetectorRF.SetGate();
				}
				int count = 0;
				while (IsRun && !(Math.Abs(RFManager.rfs.ReadGate - RFManager.rfs.Gate) < 20f) && count != 30)
				{
					Thread.Sleep(100);
					count++;
				}
				ShowMsg("模拟模式，正在采集...");
				for (int sweepIndex = 0; sweepIndex < method.Sweeps; sweepIndex++)
				{
					ShowMsg($"扫描第{runIndex + 1}次，重复次数{sweepIndex + 1}");
					if (!IsRun)
					{
						return true;
					}
					if (sweepIndex == 0)
					{
						Detector.Instance.SetMassAxis(mass);
					}
					Detector.Instance.SendHopDatas(mass, resolution1.ToArray(), start: true);
					if (!scanEvent.WaitOne(60000))
					{
						string jsontext = GenerateJson.ReturnData(1, 0, 0, 0, 0, null);
						OnStatus(jsontext);
						ShowMsg(Lang.Txt("FormExperLog.Text2"));
						Log.InsertLog(Lang.Txt("FormExperLog.Text4"), Lang.Txt("FormExperLog.Text2"), Lang.Txt("FormExperLog.Text3"), Severity.Error);
						return false;
					}
					Log.InsertLog("客户端系统", "正在采集", GenerateJson.ReturnData(0, 0, 0, 0, 0, Detector.msDetectorPara.IntesnsityPulse.ToList()), Severity.Info);
				}
				intensityANA = Detector.msDetectorPara.IntesnsityAnalog.ToList();
				RFManager.rfs.Gate = (float)((double)RFManager.rfs.HVP * 0.23);
				if (Detector.detectorConfig.IsNewDetector)
				{
					Detector.Instance.msDetectorRF.SetGateNew();
				}
				else
				{
					Detector.Instance.msDetectorRF.SetGate();
				}
			}
			CurrentSpectrums = new SpectrumInfos();
			ProcessSpectumsForNew(CurrentSpectrums, intensityPC, intensityANAforPC, intensityANA, method.Sweeps);
			ProcessMainRunDatasForNew(CurrentSpectrums, runIndex);
		}
		return true;
	}

	private void ProcessMainRunDatasForNew(SpectrumInfos spectrum, int mainIndex, int UsePA = 0)
	{
		switch (UsePA)
		{
		case 0:
		{
			for (int l = 0; l < spectrum.intensity.Count(); l++)
			{
				double pafactor = Detector.Instance.dacPAFactors[(int)(spectrum.mass[l] * 100.0)];
				if (spectrum.intensityPulse[l] < Detector.detectorConfig.PaThreshold && spectrum.intensityAnalog[l] * pafactor < Detector.detectorConfig.PaThreshold)
				{
					spectrum.intensity[l] = spectrum.intensityPulse[l];
				}
				else
				{
					spectrum.intensity[l] = spectrum.intensityAnalog[l] * pafactor;
				}
			}
			break;
		}
		case 1:
		{
			for (int j = 0; j < spectrum.intensity.Count(); j++)
			{
				spectrum.intensity[j] = spectrum.intensityPulse[j];
			}
			break;
		}
		case 2:
		{
			for (int i = 0; i < spectrum.intensity.Count(); i++)
			{
				spectrum.intensity[i] = spectrum.intensityAnalog[i];
			}
			break;
		}
		default:
		{
			for (int k = 0; k < spectrum.intensity.Count(); k++)
			{
				double pafactor2 = Detector.Instance.dacPAFactors[(int)(spectrum.mass[k] * 100.0)];
				if (spectrum.intensityPulse[k] < Detector.detectorConfig.PaThreshold && spectrum.intensityAnalog[k] * pafactor2 < Detector.detectorConfig.PaThreshold)
				{
					spectrum.intensity[k] = spectrum.intensityPulse[k];
				}
				else
				{
					spectrum.intensity[k] = spectrum.intensityAnalog[k] * pafactor2;
				}
			}
			break;
		}
		}
		string jsontext = GenerateJson.ReturnData(0, 0, 0, mainIndex + 1, spectrum.mass.Count(), spectrum.intensity.ToList());
		OnStatus(jsontext);
	}

	private void ProcessSpectumsForNew(SpectrumInfos spectrum, List<double> intensityPC, List<double> intensityANAforPC, List<double> intensityANA, int sweeps)
	{
		double[] mass = PeakMass.ToArray();
		int nlenth = mass.Length;
		spectrum.mass = new double[nlenth];
		spectrum.intensityAnalog = new double[nlenth];
		spectrum.intensityPulse = new double[nlenth];
		spectrum.intensity = new double[nlenth];
		Array.Copy(mass, spectrum.mass, nlenth);
		int tempPC = 0;
		int tempANA = 0;
		for (int i = 0; i < nlenth; i++)
		{
			if (!OutPeakMass.Contains(spectrum.mass[i]))
			{
				spectrum.intensityAnalog[i] = intensityANA[tempANA] / (double)sweeps;
				spectrum.intensityPulse[i] = 0.0;
				tempANA++;
			}
			else
			{
				spectrum.intensityAnalog[i] = intensityANAforPC[tempPC] / (double)sweeps;
				spectrum.intensityPulse[i] = intensityPC[tempPC] / (double)sweeps;
				tempPC++;
			}
		}
	}

	private void ProcessSpectums(SpectrumInfos spectrum, int runindex, int sweeps)
	{
		double[] mass = PeakMass.ToArray();
		int nlenth = mass.Length;
		spectrum.mass = new double[nlenth];
		spectrum.intensityAnalog = new double[nlenth];
		spectrum.intensityPulse = new double[nlenth];
		spectrum.intensity = new double[nlenth];
		Array.Copy(mass, spectrum.mass, nlenth);
		int temp = 0;
		for (int i = 0; i < nlenth; i++)
		{
			if (OutPeakMass.Contains(spectrum.mass[i]))
			{
				spectrum.intensityAnalog[i] = double.PositiveInfinity;
				spectrum.intensityPulse[i] = double.PositiveInfinity;
			}
			else
			{
				spectrum.intensityAnalog[i] = Detector.msDetectorPara.IntesnsityAnalog[temp] / (double)sweeps;
				spectrum.intensityPulse[i] = Detector.msDetectorPara.IntesnsityPulse[temp] / (double)sweeps;
				temp++;
			}
		}
	}

	private void ProcessMainRunDatas(SpectrumInfos spectrum, int mainIndex, int UsePA = 0)
	{
		switch (UsePA)
		{
		case 0:
		{
			for (int l = 0; l < spectrum.intensity.Count(); l++)
			{
				double pafactor = Detector.Instance.dacPAFactors[(int)(spectrum.mass[l] * 100.0)];
				if (spectrum.intensityPulse[l] < Detector.detectorConfig.PaThreshold && spectrum.intensityAnalog[l] * pafactor < Detector.detectorConfig.PaThreshold)
				{
					spectrum.intensity[l] = spectrum.intensityPulse[l];
				}
				else
				{
					spectrum.intensity[l] = spectrum.intensityAnalog[l] * pafactor;
				}
			}
			break;
		}
		case 1:
		{
			for (int j = 0; j < spectrum.intensity.Count(); j++)
			{
				spectrum.intensity[j] = spectrum.intensityPulse[j];
			}
			break;
		}
		case 2:
		{
			for (int i = 0; i < spectrum.intensity.Count(); i++)
			{
				spectrum.intensity[i] = spectrum.intensityAnalog[i];
			}
			break;
		}
		default:
		{
			for (int k = 0; k < spectrum.intensity.Count(); k++)
			{
				double pafactor2 = Detector.Instance.dacPAFactors[(int)(spectrum.mass[k] * 100.0)];
				if (spectrum.intensityPulse[k] < Detector.detectorConfig.PaThreshold && spectrum.intensityAnalog[k] * pafactor2 < Detector.detectorConfig.PaThreshold)
				{
					spectrum.intensity[k] = spectrum.intensityPulse[k];
				}
				else
				{
					spectrum.intensity[k] = spectrum.intensityAnalog[k] * pafactor2;
				}
			}
			break;
		}
		}
		string jsontext = GenerateJson.ReturnData(0, 0, 0, mainIndex + 1, spectrum.mass.Count(), spectrum.intensity.ToList());
		OnStatus(jsontext);
	}
}
